<template>
  <el-dialog :visible.sync="visible"
             :title="!dataForm.id ? `${$t('add')}角色` : `${$t('update')}角色`"
             :close-on-click-modal="false"
             :append-to-body="true"
             :before-close="closeDialog"
             class="add-or-updata"
             width="35%"
             :close-on-press-escape="false">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             label-position="top"
             @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="name">
        <el-input v-model.trim="dataForm.name"
                  :placeholder="$t('role.name')"
                  maxlength="50"
                  show-word-limit></el-input>
      </el-form-item>
      <el-form-item prop="remark">
        <el-input type="textarea"
                  :placeholder="$t('role.remark')"
                  v-model.trim="dataForm.remark"
                  maxlength="100"
                  show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button type="primary"
                 :loading="isRequest"
                 @click="dataFormSubmitHandle()"
                 :disabled="enableConfig">{{ $t('confirm') }}</el-button>
      <el-button @click="closeDialog">{{ $t('cancel') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      visible: false,
      user: {},
      isRequest: false,
      menuList: [],
      enableConfig: false, // 确认之后禁用掉按钮，等操作成功之后恢复
      mixinViewModuleOptions: {
        getDataInfoURL: '/system/sysRole/getById?id=', // 获取角色管理的详情
        addOrUpdateURL: '/system/sysRole' // 新增或添加的接口
      },
      dataForm: {
        id: '',
        name: '',
        menuIdList: [],
        remark: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    closeDialog () {
      this.visible = false
      this.isRequest = false
      this.$refs['dataForm'].resetFields()
    },
    init () {
      this.visible = true
      this.isRequest = false
      this.$nextTick(() => {
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.getDataInfo().then(res => {
        if (res.code !== 0) { return }
        this.dataForm.menuIdList.forEach(item => this.$refs.menuListTree.setChecked(item, true))
        this.$refs.deptListTree.setCheckedKeys(this.dataForm.deptIdList)
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.addOrUpdateItem().then(res => {
        if (res.code !== 0) { return }
        this.closeDialog()
        this.$emit('callBack')
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
